<?php
namespace app\backend\controller;

class Index extends Backend {
	
	public function __construct() {
		
	}
	
	public function index() {
		
		$view = \think\View::instance();
		// $request = \think\Request::instance();
		
		// 检查SESSION中保存的管理员ID
		$manager_id = \think\Session::get('manager_id');
		if(null !== $manager_id){
			// 从数据库中查询该管理员，并判断状态
			$ManagerModel = \app\common\model\ManagerModel::get($manager_id);
			if((null === $ManagerModel) || (1 === $ManagerModel->getData('is_del')) || (1 === $ManagerModel->getData('is_disable'))){
				// 状态不符，重置SESSION中保存的管理员ID，强制退回登录页面
				$manager_id = null;
				\think\Session::delete('manager_id');
			};
		};
		
		// 判断管理员ID
		if(null === $manager_id){
			
			// 生成验证码
			$captcha_obj = new \captcha\Captcha();
			$captcha_obj->imageW = 220;
			$captcha_obj->imageH = 50;
			$captcha_result = $captcha_obj->entry();
			$view->assign('captcha', 'data:image/png;base64,' . base64_encode($captcha_result['image']));
			
			\think\Session::set('captcha_code', $captcha_result['code']);
			
			// 确定视图名称
			$templete_name = 'login';
			
		}else{
			
			// 确定视图名称
			$templete_name = 'index';
			
			// 传入用户信息
			$view->assign('manager_info', $ManagerModel->toArray());
			
			// 传入菜单节点
			$view->assign('menu_node', \think\Config::get('menu_node'));
		};
		
		return $view->fetch($templete_name);
		
	}
	
	public function captcha() {
		
		$captcha_obj = new \captcha\Captcha();
		$captcha_obj->imageW = 220;
		$captcha_obj->imageH = 50;
		$captcha_result = $captcha_obj->entry();
		
		\think\Session::set('captcha_code', $captcha_result['code']);
		
		return $this->disposeJson(0, '', array(
			'captcha' => 'data:image/png;base64,' . base64_encode($captcha_result['image']),
		));
	}
	
	public function login() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('username', $parameter) && is_string($parameter['username']) && (1 === preg_match('/^[0-9a-zA-Z]{5,20}$/', $parameter['username']));
		$judge_2 = array_key_exists('password', $parameter) && is_string($parameter['password']) && (1 === preg_match('/^[0-9a-zA-Z]{6,20}$/', $parameter['password']));
		$judge_3 = array_key_exists('verify', $parameter) && is_string($parameter['verify']) && (1 === preg_match('/^[0-9a-zA-Z]{1,5}$/', $parameter['verify']));
		if(!$judge_1 || !$judge_2 || !$judge_3){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$captcha_code = \think\Session::pull('captcha_code');
		if(!is_string($captcha_code)){
			return $this->disposeJson(1, '验证码已失效');
		};
		
		$captcha_obj = new \captcha\Captcha();
		if(true !== $captcha_obj->check($parameter['verify'], $captcha_code)){
			return $this->disposeJson(1, '验证码不正确');
		};
		
		// 获取账号
		$ManagerModel = \app\common\model\ManagerModel::where('is_del', 0)->where('username', $parameter['username'])->find();
		if(null === $ManagerModel){
			return $this->disposeJson(1, '账号/密码不正确，请核实');
		};
		
		// 验证密码
		$parameter['password'] = md5($parameter['password'] . \think\Config::get('manager_password_suffix'));
		if($ManagerModel->getData('password') !== $parameter['password']){
			return $this->disposeJson(1, '账号/密码不正确，请核实');
		};
		
		// 判断账户状态
		if(1 === $ManagerModel->getData('is_disable')){
			return $this->disposeJson(1, '账户已经被禁用，请联系管理员');
		};
		
		\think\Session::set('manager_id', $ManagerModel->getData('id'));
		return $this->disposeJson(0, '登录成功，即将跳转');
	}
	
}